package com.myaihui.service.impl;


import com.myaihui.domain.Account;
import com.myaihui.domain.PropertyMgr;
import com.myaihui.foundation.constant.SysUserConstant;
import com.myaihui.foundation.model.Pager;
import com.myaihui.foundation.query.PropertyMgrQuery;
import com.myaihui.repository.PropertyMgrRepository;
import com.myaihui.service.PropertyMgrService;
import com.myaihui.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * *******************************
 * Created by Tim.
 * form com.matt
 * on 2018/5/29
 * *******************************
 */
@Service
public class PropertyMgrServiceImpl implements PropertyMgrService {

    @Autowired
    private PropertyMgrRepository propertyMgrRepository;

    @Autowired
    private SysUserService sysUserService;

    @Override
    public PropertyMgr addPropertyMgr(PropertyMgr propertyMgr) {
        return propertyMgrRepository.save(propertyMgr);
    }

    @Override
    public Pager<PropertyMgr> getPropertyMgrPage(PropertyMgrQuery propertyMgrQuery) {
        Specification<PropertyMgr> specification = new Specification<PropertyMgr>() {
            @Override
            public Predicate toPredicate(Root<PropertyMgr> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<>();

                if (StringUtils.isNotBlank(propertyMgrQuery.getName()))
                    predicates.add(criteriaBuilder.and(criteriaBuilder.like(root.get("name"), "%" + propertyMgrQuery.getName() + "%")));

                if (StringUtils.isNotBlank(propertyMgrQuery.getAddress()))
                    predicates.add(criteriaBuilder.and(criteriaBuilder.like(root.get("address"), "%" + propertyMgrQuery.getAddress() + "%")));

                query.where(predicates.toArray(new Predicate[predicates.size()]));
                query.orderBy(criteriaBuilder.desc(root.get("created")));
                return query.getRestriction();
            }
        };
        Pageable pageable = new PageRequest(propertyMgrQuery.getiDisplayStart(), propertyMgrQuery.getiDisplayLength());
        Page<PropertyMgr> page = propertyMgrRepository.findAll(specification, pageable);

        return new Pager(page, propertyMgrQuery.getsEcho(), page.getContent());
    }

    @Override
    public PropertyMgr modify(PropertyMgr propertyMgr, Account account, String username, String password, String createdId) {
        if (StringUtils.isNotBlank(propertyMgr.getId())) {
            PropertyMgr oldPropertyMgr = propertyMgrRepository.findOne(propertyMgr.getId());
            oldPropertyMgr.merge(propertyMgr);
            oldPropertyMgr.setId(propertyMgr.getId());
            oldPropertyMgr.setAccount(account);
            propertyMgrRepository.save(oldPropertyMgr);
            sysUserService.modifyPassword(SysUserConstant.PM_ADMIN, oldPropertyMgr.getId(), password);
            return oldPropertyMgr;
        } else {
            propertyMgr.setAccount(account);
            propertyMgrRepository.save(propertyMgr);
            sysUserService.add(username, password, SysUserConstant.PM_ADMIN, propertyMgr.getId(), createdId);
            return propertyMgr;
        }
    }

    @Override
    public PropertyMgr get(String id) {
        return propertyMgrRepository.findOne(id);
    }
}
